package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddLog;
import com.baizhi.dao.CategoryMapper;
import com.baizhi.dao.VideoMapper;
import com.baizhi.dto.CategoryPageDTO;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.*;
import com.baizhi.service.CategoryService;
import com.baizhi.service.VideoService;
import com.baizhi.util.AliyunOSSUtil;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class VideoServiceImpl implements VideoService {

    private static final Logger log = LoggerFactory.getLogger(VideoServiceImpl.class);
    @Resource
    VideoMapper videoMapper;

    @Override
    public CommonQueryPageVO queryAllPage(Integer page, Integer pageSize) {

        UserExample example = new UserExample();
        int total = videoMapper.selectCountByExample(example);
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        List<Video> videos = videoMapper.selectByExampleAndRowBounds(example, rowBounds);

        return new CommonQueryPageVO(page,total,videos);
    }



    @AddLog(value = "添加视频")
    @Override
    public void add(Video video) {
        video.setId(UUID.randomUUID().toString());
        video.setStatus("1");
        video.setCreateTime(new Date());
        //video.setUserId("4");
            videoMapper.insertSelective(video);
    }
    @AddLog(value = "删除视频")
    @Override
    public void delete(Video video) {

        //1. 删除文件
        //根据文件信息
        Video videol = videoMapper.selectOne(video);
        //获取图片网络路径
        String videoPath = videol.getVideoPath();
        String coverPath = videol.getCoverPath();
        //字符串处理
        String videoPathName = videoPath.replace("https://oss-cn-beijing.aliyuncs.com/ ", "");
        String coverPathName = coverPath.replace("https://oss-cn-beijing.aliyuncs.com/ ", "");

        AliyunOSSUtil.deleteFile("yingx-c",videoPathName);
        AliyunOSSUtil.deleteFile("yingx-c",coverPathName);

        //2. 删除数据
        videoMapper.delete(video);
    }

    @Override
    public Video queryById(String id) {
        return videoMapper.selectByPrimaryKey(id);
    }

    @AddLog(value = "修改视频信息")
    @Override
    public void update(Video video) {
            videoMapper.updateByPrimaryKeySelective(video);
    }

    @Override
    public List<Video> queryByLevelsVideo(Integer levels) {

        CategoryExample example = new CategoryExample();
        example.createCriteria().andLevelsEqualTo(levels);

        List<Video> videos = videoMapper.selectByExample(example);

        return videos;
    }

    @Override
    public HashMap<String, String> uploadVideoAliyun(MultipartFile videoFile) {

        //1.获取文件名
        //String fileName = videoFile.getOriginalFilename();

        String filename = videoFile.getOriginalFilename();
        //新文件名拼接时间戳
        String newName = new Date().getTime()+"-"+filename;

        //=========================================

        //配置存储空间名
        //String bucketName = "yingx-c";
        //配置文件名(文件名拼接目录)
        String objectName = "video/"+newName;

        log.info("文件名：{}",videoFile.getOriginalFilename());
        log.info("文件大小：{}",videoFile.getSize());
        log.info("文件类型：{}",videoFile.getContentType());


        //=========================================
        //.视频上传
        AliyunOSSUtil.uploadfileBytes("yingx-c",objectName,videoFile);

        //2.获取阿里云视频文件截取视频封面

        URL url = AliyunOSSUtil.videoInterceptCoverUpload("yingx-c", objectName);
        System.out.println(url);

        //通过视频名获取封面图片名
        String coverName = "videoCover/"+newName.split("\\.")[0]+".jpg";

        //3.将视频封面上传至阿里云
        try {
            AliyunOSSUtil.uploadNetIO("yingx-c",coverName,url.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*try {
            AliyunOSSUtil.videoInterceptCoverUpload("yingx-c",objectName,coverName);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //拼接视频的网络路径
        String netPath = "https://yingx-c.oss-cn-beijing.aliyuncs.com/"+objectName;
        String coverNames = "https://yingx-c.oss-cn-beijing.aliyuncs.com/"+coverName;

        HashMap<String, String> map = new HashMap<>();
        map.put("fileName",netPath);
        map.put("coverName",coverNames);

        return map;
    }


}
